/**
 * @function 时间戳转换为特定格式的日期字符串
 */
export const formatTimestamp = (timestamp) => {
	const date = new Date(timestamp); // 根据时间戳创建 Date 对象
	const year = date.getFullYear(); // 获取年份
	const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份，补0
	const day = date.getDate().toString().padStart(2, '0'); // 获取日期，补0
	const hours = date.getHours().toString().padStart(2, '0'); // 获取小时，补0
	const minutes = date.getMinutes().toString().padStart(2, '0'); // 获取分钟，补0
	const seconds = date.getSeconds().toString().padStart(2, '0'); // 获取秒钟，补0
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 返回格式化后的日期字符串
}

/**
 * @function 处理浮点小数精确到小数点后四位
 */
export const addStrings = (a, b) => {
	// 将字符串转换为数字类型
	const numA = parseFloat(a);
	const numB = parseFloat(b);

	// 如果其中有一个无法转换为数字，则返回 NaN
	if (isNaN(numA) || isNaN(numB)) {
		return "0";
	}

	// 直接相加，得到结果
	let result = numA + numB;

	// 保留四位小数，不进行四舍五入，直接截断
	result = Math.trunc(result * 10000) / 10000;
	return result;
}

/**
 * @function 复制内容
 */
export const contentCopy = (content, message = "复制成功") => {
	if (!content) return;
	uni.setClipboardData({
		data: content,
		success: (result) => {
			uni.showToast({
				icon: "none",
				title: message
			})
		}
	})
}
// 截取字符串替换为省略号
export const address2simple = (address, cut) => {
	let length = address.length;
	if (!address || length < cut * 2) {
		return "";
	}
	let prefix = address.substring(0, cut);
	let suffix = address.substring(length - cut, length);
	return `${prefix}...${suffix}`;
}

//
export const getInviteLinkCode = () => {
	const code = new URLSearchParams(location.search).get('code');
	if (code) {
		uni.setStorageSync('code', code);
		uni.reLaunch({
			url: '/pages/register/register'
		});
	}
};